package modele;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.List;

import javax.swing.JFileChooser;

import vue.PanelCK;

public class Exporter {
	
	public Exporter(){
		
	}
	
	
	public void toECG(CKModel<String, String> modele){
		TreeLayout tl = new TreeLayout(modele);
		tl.buildTree();
		
		PanelCK pnl = new PanelCK(modele);
		pnl.paint(pnl.getGraphics());
		
		JFileChooser fc = new JFileChooser();
		File file = new File("default.eck");
		int returnVal = fc.showSaveDialog(fc);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
	            file = fc.getSelectedFile();
	            if(!file.getName().toLowerCase().endsWith(".eck")){
	            	String filePath = file.getPath().concat(".eck");
	            	file = new File(filePath);
	            } else {
	            	file = new File(file.getPath());
	            }
	            
	         
				try {
					// On instancie le flot d'ecriture
					FileWriter fw = new FileWriter(file);
					
					/*
					 * A cet emplacement on écrira tous les concepts
					 */
					fw.write("DebutConcept"+ System.getProperty("line.separator"));
					fw.write(modele.getCSize() + System.getProperty("line.separator") + System.getProperty("line.separator"));
					
					for ( int i = 0; i < modele.getCSize(); i++){
						fw.write(modele.findConceptFromId(i).getIndex() + System.getProperty("line.separator"));
						fw.write(forHTML(modele.findConceptFromId(i).getData()) +System.getProperty("line.separator"));
						fw.write(modele.findConceptFromId(i).getPositionX() + System.getProperty("line.separator"));
						fw.write(modele.findConceptFromId(i).getPositionY() + System.getProperty("line.separator") + System.getProperty("line.separator"));
					}
					
					/*
					 * A cet emplacement on écrira tous les Knowledges
					 */
				
					fw.write("DebutKnowledge"+ System.getProperty("line.separator"));
					fw.write(modele.getKSpace().size() + System.getProperty("line.separator") + System.getProperty("line.separator"));
					for ( int i = 0; i < modele.getKSpace().size(); i++){
						fw.write(modele.getKSpace().elementAt(i).getIndex() + System.getProperty("line.separator"));
						fw.write(forHTML(modele.getKSpace().elementAt(i).getData()) +System.getProperty("line.separator"));
						fw.write(modele.getKSpace().elementAt(i).getPositionX()  + System.getProperty("line.separator"));
						fw.write(modele.getKSpace().elementAt(i).getPositionY()  + System.getProperty("line.separator") + System.getProperty("line.separator"));
					}
					
					fw.write("DebutEdge"+ System.getProperty("line.separator"));
					fw.write(modele.getCSize() - 1 + System.getProperty("line.separator") + System.getProperty("line.separator"));
					int ind = 0;
					for ( int i = 0; i < modele.getCSize() - 1; i++){
						
						List<Concept<String>> children = modele.findConceptFromId(i).getChildren();
						for (int j = 0; j < children.size(); j++){
							
							fw.write(ind + System.getProperty("line.separator"));
							fw.write(modele.findConceptFromId(i).getIndex() + System.getProperty("line.separator"));
							fw.write(children.get(j).getIndex() + System.getProperty("line.separator") + System.getProperty("line.separator"));
							ind++;
						}
					}
					fw.close();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
	}
	
	
	
	
	
	
	public static String forHTML(String aText){
	     final StringBuilder result = new StringBuilder();
	     final StringCharacterIterator iterator = new StringCharacterIterator(aText);
	     char character =  iterator.current();
	     while (character != CharacterIterator.DONE ){
	       if (character == '<') {
	         result.append("&lt;");
	       }
	       else if (character == '>') {
	         result.append("&gt;");
	       }
	       else if (character == '&') {
	         result.append("&amp;");
	      }
	       else if (character == '\"') {
	         result.append("&quot;");
	       }
	       else if (character == '\t') {
	         addCharEntity(9, result);
	       }
	       else if (character == '!') {
	         addCharEntity(33, result);
	       }
	       else if (character == '#') {
	         addCharEntity(35, result);
	       }
	       else if (character == '$') {
	         addCharEntity(36, result);
	       }
	       else if (character == '%') {
	         addCharEntity(37, result);
	       }
	       else if (character == '\'') {
	         addCharEntity(39, result);
	       }
	       else if (character == '(') {
	         addCharEntity(40, result);
	       }
	       else if (character == ')') {
	         addCharEntity(41, result);
	       }
	       else if (character == '*') {
	         addCharEntity(42, result);
	       }
	       else if (character == '+') {
	         addCharEntity(43, result);
	       }
	       else if (character == ',') {
	         addCharEntity(44, result);
	       }
	       else if (character == '-') {
	         addCharEntity(45, result);
	       }
	       else if (character == '.') {
	         addCharEntity(46, result);
	       }
	       else if (character == '/') {
	         addCharEntity(47, result);
	       }
	       else if (character == ':') {
	         addCharEntity(58, result);
	       }
	       else if (character == ';') {
	         addCharEntity(59, result);
	       }
	       else if (character == '=') {
	         addCharEntity(61, result);
	       }
	       else if (character == '?') {
	         addCharEntity(63, result);
	       }
	       else if (character == '@') {
	         addCharEntity(64, result);
	       }
	       else if (character == '[') {
	         addCharEntity(91, result);
	       }
	       else if (character == '\\') {
	         addCharEntity(92, result);
	       }
	       else if (character == ']') {
	         addCharEntity(93, result);
	       }
	       else if (character == '^') {
	         addCharEntity(94, result);
	       }
	       else if (character == '_') {
	         addCharEntity(95, result);
	       }
	       else if (character == '`') {
	         addCharEntity(96, result);
	       }
	       else if (character == '{') {
	         addCharEntity(123, result);
	       }
	       else if (character == '|') {
	         addCharEntity(124, result);
	       }
	       else if (character == '}') {
	         addCharEntity(125, result);
	       }
	       else if (character == '~') {
	         addCharEntity(126, result);
	       }
	       else {
	         //the char is not a special one
	         //add it to the result as is
	         result.append(character);
	       }
	       character = iterator.next();
	     }
	     return result.toString();
	  }

	
	private static void addCharEntity(Integer aIdx, StringBuilder aBuilder){
	    String padding = "";
	    if( aIdx <= 9 ){
	       padding = "00";
	    }
	    else if( aIdx <= 99 ){
	      padding = "0";
	    }
	    else {
	      //no prefix
	    }
	    String number = padding + aIdx.toString();
	    aBuilder.append("&#" + number + ";");
	  }
	

}
